package com.desay.pangoo.itmt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.desay.pangoo.common.lite.lang.web.BusinessException;
import com.desay.pangoo.common.lite.lang.web.CommonResponse;
import com.desay.pangoo.itmt.controller.vo.ProjectBUVo;
import com.desay.pangoo.itmt.enums.ExceptionCodeEnum;
import com.desay.pangoo.itmt.service.ProjectBuService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * BU信息维护
 *
 * @author uidq1163
 */
@RestController
@Api(tags = "ITMT-项目平台BU信息")
@RequestMapping(value = "/project/bu")
@Validated
public class ProjectBuController {
	@Autowired
	private ProjectBuService service;

	@PostMapping
	@ApiOperation(value = "添加信息", notes = "")
	public ResponseEntity<Object> create(@RequestBody ProjectBUVo vo) {
		Long id = service.create(vo);
		return new ResponseEntity<>(CommonResponse.success(id), HttpStatus.OK);
	}

	@DeleteMapping(value = "{id}")
	@ApiOperation(value = "删除", notes = "删除信息")
	@ApiImplicitParam(name = "id", value = "删除信息", required = true, dataTypeClass = Long.class, paramType = "path")
	public ResponseEntity<Object> delete(@PathVariable long id) {
		try {
			service.delete(id);
		} catch (DataIntegrityViolationException e) {
			throw new BusinessException(ExceptionCodeEnum.PROJECT_HAS_USED);
		}
		return new ResponseEntity<>(CommonResponse.success(), HttpStatus.OK);
	}

	@PutMapping(value = "{id}")
	@ApiOperation(value = "更新", notes = "更新信息")
	public ResponseEntity<Object> update(@PathVariable(value = "id") Long id, @RequestBody ProjectBUVo vo) {
		service.update(id, vo);
		return new ResponseEntity<>(CommonResponse.success(), HttpStatus.OK);
	}

	@GetMapping(value = "")
	@ApiOperation(value = "获取分页", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "platform", type = "string", value = "平台", paramType = "query"),
			@ApiImplicitParam(name = "classification", type = "string", value = "分类", paramType = "query"),
			@ApiImplicitParam(name = "pageNo", type = "int", value = "指定查询页数", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", type = "int", value = "每页数据数量", paramType = "query") })
	public ResponseEntity<Object> getAllList(@RequestParam(value = "platform", required = false) String platform,
			@RequestParam(value = "classification", required = false) String classification,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
		return new ResponseEntity<>(CommonResponse.success(service.getAll(platform, classification, pageSize, pageNo)),
				HttpStatus.OK);
	}
}
